<template>
    
    <!-- 组件的模板 -->
    <div class="page team-info">
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">企业全称</label></div>
                <div class="weui-cell__bd" id="myInput">
                    <input class="weui-input too_much" maxlength="50" type="text" placeholder="请按照营业执照名称填写" v-model="team_detailname" :value="team_detailname">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">企业简称</label></div>
                <div class="weui-cell__bd" id="myInput">
                    <input class="weui-input" maxlength="20" type="text" placeholder="如：斗米" v-model="team_name" :value="team_name">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">联系人</label></div>
                <div class="weui-cell__bd" id="myInput">
                    <input class="weui-input" maxlength="10" type="text" placeholder="请输入" v-model="linkman_name" :value="linkman_name">
                </div>
            </div>
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">手机号码</label></div>
                <div class="weui-cell__bd" id="myInput">
                    <input class="weui-input" maxlength="20" type="tel" placeholder="请输入" v-model="linkman_number" :value="linkman_number">
                </div>
            </div>
        </div>  
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="industrySelect(obj.industry)">
                <div class="weui-cell__bd">
                    <p v-if="obj.industry == '请选择'"><span class="title">行业</span><span class="placeholder">{{obj.industry}}</span></p>
                    <p v-else><span class="title">行业</span>{{obj.industry}}</p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a> 
            <a class="weui-cell weui-cell_access" href="dmsaas://SelectProvince">
                <div class="weui-cell__bd">
                    <p v-if="cname == '请选择'"><span class="title">所在地区</span><span class="placeholder">{{cname}}</span></p>
                    <p v-else><span class="title">地区</span>{{cname}} {{dname}}</p>
                </div>
                <div class="weui-cell__ft"></div>
            </a> 
            <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label go_top">详细地址</label></div>
                <!-- <div class="weui-cell__bd" id="myInput">
                    <input class="weui-input more_height" maxlength="50" type="text" placeholder="请输入" v-model="team_detailaddr" :value="team_detailaddr">
                </div> -->
                <div class="weui-cell__bd" id="myInput">
                    <textarea 
                        class="weui-textarea" 
                        placeholder="请输入" 
                        rows="2" 
                        maxlength="50" 
                        v-model="team_detailaddr"
                    ></textarea>
                </div>
            </div>
        </div>
    </div>

</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import clientUi from 'gapi/clientUi.js'
    import widget from 'gapi/widget.js'
    import * as util from 'js/util.js'
    import teamApi from 'clientApi/teamApi.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
     let teamid = ''
     let super_id = ''
     let old = ''

    /**
     *  组件实例对象
     */
    export default {
        components: {

        },
        data: function(){
            return{
                team_name:'',  // 团队简称
                obj:{  // 行业进行选择的默认数据
                    industry:'请选择',
                    parentID:'',
                    childID:'',
                    parentText:'',
                    childText:''
                },
                industryData:[],  //存放行业所有从缓存中取到的下拉数据
                pname:'',
                cname:'',
                dname:'',
                pid:'',
                cid:'',
                did:'',
                team_detailname: '',//企业全称
                linkman_name: '',//企业联系人
                linkman_number: '',//联系人手机号
                team_detailaddr: '',//详细地址
                company_id: '',
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void 
             */
            init (){
                
                this.disabled = util.getUrddataString('disabled')
                teamid = util.getUrddataString('teamId')
                super_id = util.getUrddataString('super_id')
                let baseid = {
                    team_id:teamid,
                    super_id:super_id, 
                    project_id:0
                }
                cache.setCache('KqCacheMessge', baseid, 86400*7)
                //通过键值获取由setCache设置的本地存储的行业数据
                cache.getCache('str', (data) => {
                    for( let o in data){
                        this.industryData.push(data[o])
                    }
                })
                commonPage.getWebViewID((data)=>{
                    cache.setMemoryCache('webViewID',{'webViewID':data})
                })
                
                
                cache.deleteCache('addressArrData')
                window.loadData = (data) => {
                    if(data.type){
                        // this.obj.industry = data.value.ptext + ' ' + data.value.ctext
                        this.obj.industry = data.value.ctext
                        this.obj.parentText = data.value.ptext
                        this.obj.childText = data.value.ctext
                        this.obj.parentID = data.key.industrypid
                        this.obj.childID = data.key.industrycid
                    }else{
                        this.pid = data.key.province_id
                        this.cid = data.key.city_id
                        this.did = data.key.district_id
                        this.pname = data.value.province_name
                        this.cname = data.value.city_name
                        this.dname = data.value.district_name
                        cache.setCache('addressArrData', data, 86400*30) 
                    }
                }

                /*获取团队信息*/
                util.ajax({
                    url:'/team/info',
                    type:'GET',
                    data:{
                        team_id:teamid,
                        project_id:0,
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log("获取团队信息"+JSON.stringify(obj))
                        old = obj.data
                        if(obj&&obj.errno == 0){
                            this.company_id = obj.data.company_id
                            this.team_name = obj.data.team_name;
                            this.team_detailname = obj.data.name;
                            this.linkman_name = obj.data.contact_person;
                            this.linkman_number = obj.data.mobile;
                            this.team_detailaddr = obj.data.address;
                            if(obj.data.industry_1st_name == '' || obj.data.industry_2st_name == ''){
                                this.obj.industry = '请选择'
                            }else{
                                this.obj.parentID = obj.data.industry_1st_id;
                                this.obj.childID = obj.data.industry_2st_id;
                                this.obj.parentText = obj.data.industry_1st_name;
                                this.obj.childText = obj.data.industry_2st_name;
                                this.obj.industry = this.obj.childText
                            }
                            if(obj.data.addr_province_name == '' || obj.data.addr_city_name == '' || obj.data.addr_district_name == ''){
                                this.cname = '请选择'
                            }else{
                                this.pid = obj.data.addr_province_id
                                this.cid = obj.data.addr_city_id
                                this.did = obj.data.addr_district_id
                                this.pname = obj.data.addr_province_name
                                this.cname = obj.data.addr_city_name
                                this.dname = obj.data.addr_district_name
                            }
                            
                            
                            cache.deleteCache('addressArrData')
                            let addressKey = {
                                province_id:obj.data.province_id,
                                city_id:obj.data.city_id,
                                district_id:obj.data.district_id
                            }
                            let addressVal = {
                                province_name:obj.data.addr_province_name,
                                city_name:obj.data.addr_city_name,
                                district_name:obj.data.addr_district_name
                            }
                            let saveData = {
                                key:addressKey,
                                value:addressVal
                            }
                            cache.setCache('addressArrData', saveData, 86400*30)

                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                }) 
            },
            /*行业选择*/
            industrySelect (industryName){
                let encodeUrl = encodeURIComponent(`industryName=${industryName}`)
                location.href = `dmsaas://SelectIndustry?urdData=${encodeUrl}`
            }
        },
        created(){

            this.init();

            /*设置Native的头部*/
            commonPage.updateTitleBar({
                rightDisplay: true,            // 是否显示右按钮
                rightText: '保存',            // 右按钮文案
                rightAction: 'saveResume'    // 点击右按钮的文案（即回调函数名称）
            })

            //保存
            window.saveResume = () => {
                var that = this;
                if(that.team_detailname == ''){
                    widget.toast('企业全称不能为空')
                    return
                }
                if(that.team_name == ''){
                    widget.toast('企业简称不能为空')
                    return
                }
                if(that.linkman_name == ''){
                    widget.toast('联系人不能为空')
                    return
                }
                if(that.linkman_number == ''){
                    widget.toast('手机号码不能为空')
                    return
                }
                if(!(/^\d{11}$/.test(that.linkman_number))){
                    widget.toast('手机号格式错误')
                    return                    
                }
                if(this.obj.parentText == '请选择' || this.obj.childText == '请选择'){
                    widget.toast('请选择行业')
                    return
                }
                if(this.cname == '请选择' || this.pname == '' || this.dname == ''){
                    widget.toast('请选择地区')
                    return
                }
                if(that.team_detailaddr == ''){
                    widget.toast('详细地址不能为空')
                    return
                }
                
                let data = {
                    team_id: teamid,
                    project_id: 0,
                    team_name:that.team_name,
                    industry_1st_id: this.obj.parentID,
                    industry_1st_name: this.obj.parentText,
                    industry_2st_id: this.obj.childID,
                    industry_2st_name: this.obj.childText,
                    addr_province_id:this.pid,
                    addr_province_name:this.pname,
                    addr_city_id: this.cid,
                    addr_city_name: this.cname,
                    addr_district_id: this.did,
                    addr_district_name: this.dname,
                    name:this.team_detailname,
                    contact_person:this.linkman_name,
                    mobile:this.linkman_number,
                    address:this.team_detailaddr,
                    company_id:this.company_id
                }
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/team/info/edit',
                    type:'POST',
                    data:data,
                    timeout:10000,
                    success:(obj) => {
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj.errno == 0){
                            //埋点
                            let statisticsLog_data = {
                                event_id: '企业编辑-保存',
                                // event_label: data.event_label,(预留，现在不用传此参数)
                                event_param: {
                                    ca_from: 'save',
                                }
                            }
                            // alert(JSON.stringify(statisticsLog_data))
                            KCApiApp.statisticsLog(statisticsLog_data)

                            that.isshow = false;
                            widget.toast('保存成功')
                            teamApi.updateTeam(teamid);
                            commonPage.goBack(1)
                       }else{
                            widget.toast(obj.errmsg)
                       }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
            
        }
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/team.css"></style>
<style>
    div.weui-cell__ft{text-align: left;color: #333333;}
    .x-style{float: left;height: 44px;line-height: 44px;width: 32%;position: relative;}
    .weui-cell_select .weui-cell__bd:after{border-style:none;}
    .weui-cell_select .weui-cell__bd .x-style:after{
        content: " ";
        display: inline-block;
        height: 6px;
        width: 6px;
        border-width: 0px 2px 2px 0px;
        border-color: #C8C8CD;
        border-style: solid;
        -webkit-transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
        transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
        position: relative;
        top: -2px;
        position: absolute;
        top: 50%;
        right: 12px;
        margin-top: -3px;
    }
    .too_much {
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }
    .go_top {
        position: relative;
        top: -12px;
    }
</style>





